package mi.payment.ui.admin;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.settings.TerminalKeyDataOperations;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.title.Title;

import static mi.payment.settings.TerminalKeyManagementBean.SINGLE_DES;
import static mi.payment.settings.TerminalKeyManagementBean.THREE_DES;

/**
 * 下载管理界面
 * Created by lj on 2017/1/7.
 */
public class DesAlgorithmsActivity extends ParentActivity implements View.OnClickListener {



    private ImageView singleDesImage,threeDesImage;
    private TerminalKeyDataOperations mTerminalKeyDataOperations;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_des_algorithms);
        mTerminalKeyDataOperations = TerminalKeyDataOperations.getInstance();
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        Title mTitle;
        View singleDesView, threeDesView;
        mTitle = (Title) findViewById(R.id.des_algorithms_title);
        singleDesView = findViewById(R.id.single_des);
        threeDesView = findViewById(R.id.three_des);

        singleDesView.setOnClickListener(this);
        threeDesView.setOnClickListener(this);
        TextView itemLeftText,itemRightText;
        itemRightText = (TextView) singleDesView.findViewById(R.id.item_right_text);
        itemRightText.setVisibility(View.GONE);

        itemRightText = (TextView) threeDesView.findViewById(R.id.item_right_text);
        itemRightText.setVisibility(View.GONE);

        itemLeftText = (TextView) singleDesView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.single_des));

        itemLeftText = (TextView) threeDesView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.three_des));

        singleDesImage = (ImageView) singleDesView.findViewById(R.id.enter_img);
        threeDesImage = (ImageView) threeDesView.findViewById(R.id.enter_img);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.terminal_key_des_algorithms));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DesAlgorithmsActivity.this.finish();
            }
        }, null, null, null);

        updateUI();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.single_des:
                //单DES
                mTerminalKeyDataOperations.saveDes(SINGLE_DES);
                updateUI();
                break;
            case R.id.three_des:
                mTerminalKeyDataOperations.saveDes(THREE_DES);
                updateUI();
                //3DES
                break;
        }
    }

    private void updateUI(){
        if(TextUtils.isEmpty(mTerminalKeyDataOperations.getDes())){
            singleDesImage.setVisibility(View.GONE);
            threeDesImage.setVisibility(View.GONE);
        }else if(mTerminalKeyDataOperations.getDes().equals(SINGLE_DES)){
            singleDesImage.setVisibility(View.VISIBLE);
            singleDesImage.setImageResource(R.drawable.checked);
            threeDesImage.setVisibility(View.GONE);
        }else if(mTerminalKeyDataOperations.getDes().equals(THREE_DES)){
            threeDesImage.setVisibility(View.VISIBLE);
            threeDesImage.setImageResource(R.drawable.checked);
            singleDesImage.setVisibility(View.GONE);
        }
    }
}
